package com.mypack.crm.controller.contact;

import com.github.pagehelper.PageInfo;
import com.mypack.crm.constants.EnumDeleteFlag;
import com.mypack.crm.constants.EnumResponse;
import com.mypack.crm.dto.contact.ContactDTO;
import com.mypack.crm.dto.contact.ContactSearchDTO;
import com.mypack.crm.service.contact.ContactService;
import com.mypack.crm.utils.Result;
import com.mypack.crm.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/contact")
public class ContactController {

    @Autowired
    private ContactService contactService;

    @RequestMapping("/list")
    public Result list(@RequestBody ContactSearchDTO searchDTO) {

        List<ContactDTO> list = contactService.list(searchDTO, true);

        return ResultUtils.success(new PageInfo<>(list));
    }

    @RequestMapping("/add")
    public Result add(@RequestBody ContactDTO contactDTO) {

        contactService.add(contactDTO);

        return ResultUtils.success();
    }


    @RequestMapping("/update")
    public Result update(@RequestBody ContactDTO contactDTO) {

        if(contactDTO.getId() == null){
            return ResultUtils.fail(EnumResponse.INVALID_PARAM);
        }

        contactService.update(contactDTO);

        return ResultUtils.success();
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {


        ContactDTO contactDTO = new ContactDTO();
        contactDTO.setId(id);
        contactDTO.setDeleteFlag(EnumDeleteFlag.YES.getCode());

        contactService.update(contactDTO);

        return ResultUtils.success();
    }

}
